/****************************************************************************
 * Copyright 2010 Huascar A. Sanchez                                        *
 *                                                                          *
 * Licensed under the Apache License, Version 2.0 (the "License");          *
 * you may not use this file except in compliance with the License.         *
 * You may obtain a copy of the License at                                  *
 *                                                                          *
 *     http://www.apache.org/licenses/LICENSE-2.0                           *
 *                                                                          *
 * Unless required by applicable law or agreed to in writing, software      *
 * distributed under the License is distributed on an "AS IS" BASIS,        *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
 * See the License for the specific language governing permissions and      *
 * limitations under the License.                                           *
 ****************************************************************************/
package com.gotobject.greatq;

import com.gotobject.filter.internal.ToStrings;
import org.jiql.jdbc.ResultSet;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.gotobject.filter.internal.Pair.of;
import static com.gotobject.filter.internal.ToStrings.str;
import static java.util.Collections.unmodifiableList;

/**
 * @author huascar.sanchez@gmail.com (Huascar A. Sanchez)
 */
public class ResultPackage {
    private final Map<String, List<ResultSet>> content;
    private ResultPackage(){
        content = new HashMap<String, List<ResultSet>>();
    }

    public static ResultPackage empty(){
        return new ResultPackage();
    }

    public static ResultPackage prepopulated(String key, List<ResultSet> set){
        final ResultPackage temp = empty();
        temp.putAll(key, set);
        return temp;
    }

    public ResultPackage cleanCopy(ResultPackage other){
        clear();
        return copy(other);
    }

    public ResultPackage copy(ResultPackage other){
        for(String each : other.content.keySet()){
            putAll(each, other.get(each));
        }

        return this;
    }

    void putAll(String key, List<ResultSet> set){
        content.put(key, set);
    }

    public List<ResultSet> putSingle(String key){
        // pre: if it contains the key then it must have a non-null arraylist of results
        if(content.containsKey(key)){
            return content.get(key);
        } else {
            content.put(key, new ArrayList<ResultSet>());
            return content.get(key);
        }
    }

    public List<ResultSet> get(String key){
        if(!content.containsKey(key)) return Collections.emptyList();
        return unmodifiableList(content.get(key));
    }

    public boolean isEmpty(){
        return content.isEmpty();
    }

    public void clear(){
        content.clear();
    }

    public void emptyResultGroup(String key){
        if(content.containsKey(key)){
            content.get(key).clear();
        }
    }


    @Override
    public String toString() {
        return ToStrings.toString(this, of("content", str(content)));
    }
}
